//
//  NRWShareTemplateView.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWShareTemplateView.h"
#import "CarBaseInfoModel.h"
#import <CoreImage/CoreImage.h>
#import "NRWCarListsModel.h"

@interface NRWShareTemplateView()
@property (weak, nonatomic) IBOutlet UIImageView *carImageView;
@property (weak, nonatomic) IBOutlet UILabel *shopNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *carTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *carLicenceLabel;
@property (weak, nonatomic) IBOutlet UILabel *carMileageLabel;
@property (weak, nonatomic) IBOutlet UILabel *carModelEnviron;
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;
@end

@implementation NRWShareTemplateView

- (instancetype)init
{
    self = [[NSBundle mainBundle] loadNibNamed:@"NRWShareTemplateView" owner:self options:nil].firstObject;;
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.carImageView.layer.cornerRadius = 6;
    self.carImageView.layer.masksToBounds = YES;
    
    self.layer.borderColor = MAIN_NAV_COLOR().CGColor;
    self.layer.borderWidth = 5;
}

- (void)setResultModel:(Result *)resultModel {
    
    _resultModel = resultModel;
    
    self.shopNameLabel.text = resultModel.shop.shopName;
    self.nameLabel.text = [NRWCarUser sharedUser].userName;
    self.phoneLabel.text = [NRWCarUser sharedUser].userMobile;
    self.carTitleLabel.text = resultModel.carTitle;
    
    self.carLicenceLabel.text = [NSString getCarLicenceDate:resultModel.carLicenceDate carLicenceState:resultModel.carLicenceState];
    
    self.carMileageLabel.text = [NSString getCarMileage:resultModel.carMileage];
    
    self.carModelEnviron.text = isEmptyOrNilString(resultModel.carEnviron)? @"--": resultModel.carEnviron;
    self.codeImageView.image = [self getCodeImageWithMarketId:resultModel.marketId shopId:resultModel.shopId carId:resultModel.carId];
}

- (void)setModel:(CarBaseInfoModel *)model {
    
    _model = model;
    
    self.shopNameLabel.text = model.result.shop.shopName;
    self.nameLabel.text = [NRWCarUser sharedUser].userName;
    self.phoneLabel.text = [NRWCarUser sharedUser].userMobile;
    self.carTitleLabel.text = model.result.carTitle;
    
    self.carLicenceLabel.text = [NSString getCarLicenceDate:model.result.carLicenceDate carLicenceState:model.result.carLicenceState];
    
    self.carMileageLabel.text = [NSString getCarMileage:model.result.carMileage];
    
    self.carModelEnviron.text = isEmptyOrNilString(model.result.carEnviron)? @"--": model.result.carEnviron;
    self.codeImageView.image = [self getCodeImageWithMarketId:model.result.marketId shopId:model.result.shopId carId:model.result.carId];
}

- (void)setCarImage:(UIImage *)carImage {
    
    _carImage = carImage;
    
    self.carImageView.image = carImage;
}

- (UIImage *)getCodeImageWithMarketId:(NSString *)marketId shopId:(NSString *)shopId carId:(NSString *)carId {
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSData *stringData = [[NSString stringWithFormat:@"%@/mshop/src/cars_share.html?marketId=%@&shopId=%@&imgUrl=%@&customerShopId=false&carId=%@&os=false&shareUserName=%@&shareUserMobile=%@", Api, marketId, shopId, ImageApi, carId, [NRWCarUser sharedUser].userName , [NRWCarUser sharedUser].userMobile] dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:stringData forKeyPath:@"inputMessage"];
    CIImage *image = [filter outputImage];
    
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = 1;
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

- (UIImage *)getTemplateImage {
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(375, 667), NO, 0.0);
    self.layer.drawsAsynchronously = YES;
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
